import {redirectToLogin} from '@/utils/redirectToLoginPage';
import Regexps from '@/utils/reg';
import type {ProFormInstance} from '@ant-design/pro-components';
import {ModalForm, ProFormText} from '@ant-design/pro-components';
import {useRequest} from '@umijs/max';
import {message} from 'antd';
import React, {useEffect, useRef, useState} from 'react';
import {changePassword} from '@/pages/organization/user-list/service'

type Password = {
    old_pwd: string;
    new_pwd: string;
    repeat_new_pwd?: string;
};

type ResetPwdProps = {
    modalVisible: boolean;
    onClose: () => void;
};

const ResetPwd: React.FC<ResetPwdProps> = (props) => {
    const {modalVisible, onClose} = props;
    const [visible, setVisible] = useState(modalVisible);
    const formRef = useRef<ProFormInstance<Password>>();
    useEffect(() => {
        setVisible(modalVisible);
    }, [modalVisible]);
    const {run: onFinish} = useRequest(
        (params: Password) => {
            const {repeat_new_pwd, ...formData} = params;
            return changePassword(formData);
        },
        {
            manual: true,
            onSuccess: () => {
                message.success('密码重置成功');
                formRef.current?.resetFields();
                onClose();
                redirectToLogin();
            },
        },
    );

    return (
        <ModalForm<Password>
            open={visible}
            title="密码重置"
            formRef={formRef}
            autoFocusFirstInput
            layout="horizontal"
            onFinish={onFinish}
            width={560}
            labelCol={{style: {width: 120}}}
            onOpenChange={(vis) => {
                if (!vis) {
                    onClose();
                    formRef.current?.resetFields();
                }
            }}
        >
            <ProFormText.Password
                width="md"
                label="当前密码"
                name="old_pwd"
                placeholder="请输入当前密码"
                rules={[{required: true}]}
            />
            <ProFormText.Password
                width="md"
                label="新密码"
                name="new_pwd"
                placeholder="请输入新密码"
                validateTrigger={['onSubmit', 'onBlur']}
                rules={[
                    {validateTrigger: 'onSubmit', required: true},
                    ({getFieldValue}) => ({
                        validateTrigger: 'onBlur',
                        // validator(rule, value) {
                        //   if (!value || getFieldValue('old_pwd') !== value) {
                        //     return Promise.resolve();
                        //   }
                        //   return Promise.reject(
                        //     new Error("密码输入不符合规则"),
                        //   );
                        // },
                    }),
                    () => ({
                        validateTrigger: 'onSubmit',
                        // validator(rule, value) {
                        //   if (!value || Regexps.PASSWORD.test(value)) {
                        //     return Promise.resolve();
                        //   }
                        //   return Promise.reject(
                        //     new Error("密码不符合规则"),
                        //   );
                        // },
                    }),
                ]}
            />

            <ProFormText.Password
                width="md"
                label="确认密码"
                name="repeat_new_pwd"
                placeholder="请再次确认密码"
                validateTrigger={['onSubmit', 'onBlur']}
                rules={[
                    {validateTrigger: 'onSubmit', required: true},
                    ({getFieldValue}) => ({
                        validateTrigger: 'onBlur',
                        validator(rule, value) {
                            if (!value || getFieldValue('new_pwd') === value) {
                                return Promise.resolve();
                            }
                            return Promise.reject(
                                new Error("确认密码不一致"),
                            );
                        },
                    }),
                ]}
            />
        </ModalForm>
    );
};

export default ResetPwd;
